package bibbletex.helpers;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailHelper {
	private static String app_email = "xcerpt.it2012@gmail.com",
		   app_email_password = "alicewithout1",
		   email_host = "smtp.gmail.com",
		   email_port = "465",
		   socket_factory_class = "javax.net.ssl.SSLSocketFactory";

	public synchronized static boolean sendMail(String to, String subject, String text) {
		Properties props = new Properties();
		// Properties props=System.getProperties();
		props.put("mail.smtp.user", app_email);
		props.put("mail.smtp.host", email_host);
		if (!"".equals(email_port))
			props.put("mail.smtp.port", email_port);
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.debug", "false");
		props.put("mail.smtp.socketFactory.port", email_port);
		props.put("mail.smtp.socketFactory.class", socket_factory_class);
		props.put("mail.smtp.socketFactory.fallback", "false");

		try {
			Session session = Session.getDefaultInstance(props, null);
			session.setDebug(true);
			MimeMessage msg = new MimeMessage(session);
			msg.setContent(text, "text/html");
			msg.setSubject(subject);
			msg.setFrom(new InternetAddress("xcerpt.it2012@gmail.com"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			msg.saveChanges();
			Transport transport = session.getTransport("smtp");
			transport.connect(email_host, app_email, app_email_password);
			transport.sendMessage(msg, msg.getAllRecipients());
			transport.close();
			return true;
		} catch (Exception mex) {
			mex.printStackTrace();
			return false;
		}
	}

}
